// pages/my/my.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: 0,
    mybackground:null,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var height = that.data.height
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.screenHeight
        })
      },
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    wx.setStorage({
      key: 'mybackground',
      data: 'http://oymq1q5qi.bkt.clouddn.com/aybsbeijing.png',
      success: function () {
        console.log("缓存成功")
      }
    })
    var that = this
    var mybackground = that.data.mybackground
    if (mybackground==null){
wx.getStorage({
  key: 'mybackground',
  success: function(res) {
    that.setData({
      mybackground:res.data
    })
  },
})
    }
    

  },
  myorder:function(){
wx.navigateTo({
  url: '../../pages/myorder/myorder',
  success: function(res) {},
  fail: function(res) {},
  complete: function(res) {},
})
  },
  getUserInfo1: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  getUserInfo: function (e) {
    var that = this


    console.log(e.detail)
    wx.login({
      success: function (res) {

        // var code = that.data.code
        var code = res.code;//发送给服务器的code 

        wx.getUserInfo({
          withCredentials: true,
          success: function (res) {
            var encryptedData = res.encryptedData
            var iv = res.iv
            app.globalData.userInfo = res.userInfo
            that.setData({
              userInfo: res.userInfo,
              hasUserInfo: true
            })


            wx.request({

              url: 'https://ysh1998.cc/PHP/demo.php?code&encryptedData&iv',
              data: {
                code: code,
                encryptedData: res.encryptedData,
                iv: res.iv
              },
              header: {
                'content-type': 'application/json'
              },
              method: "GET",
              success: function (result) {
                console.log(result.data)

              }
            })

          }
          ,
          fail: function () {
            console.log("大一")
          }

        })

      }
    })





  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})